package org.jeecg.modules.wlhy.mbshipping.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.third.daikai.mabang.dto.MbDriverDTO;
import org.jeecg.modules.third.daikai.mabang.service.MaBangApiService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.mbshipping.entity.HyMbDriver;
import org.jeecg.modules.wlhy.mbshipping.mapper.HyMbDriverMapper;
import org.jeecg.modules.wlhy.mbshipping.service.IHyMbDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: hy_mb_driver
 * @Author: duanlikao
 * @Date:   2022-12-19
 * @Version: V1.0
 */
@Service
public class HyMbDriverServiceImpl extends ServiceImpl<HyMbDriverMapper, HyMbDriver> implements IHyMbDriverService {


    @Autowired
    private IHyDriverService hyDriverService;

    @Resource
    private MaBangApiService maBangApiService;

    @Override
    public  boolean uploadDriver(String driverId){

        HyDriver driver = hyDriverService.getById(driverId);
        if(driver == null){
            throw new JeecgBootException("该司机上报信息不能为空");
        }

        MbDriverDTO paramDTO =  this.driverTOMbDriverDTO(driver) ;

        String res = maBangApiService.driverPush( paramDTO);

        HyMbDriver hyMbDriver = new HyMbDriver();
        hyMbDriver.setDriverId(driverId);
        hyMbDriver.setUploadTime( new Date());

        if(StringUtils.isBlank(res)){
            hyMbDriver.setUploadStatus("2");
            hyMbDriver.setUploadFailureReason("推送异常");
            this.saveOrUpdateMbDriver(hyMbDriver);
            throw new JeecgBootException("该司机推送异常！");
        }else{
            Result r = JSONObject.parseObject(res,Result.class);

            if(500 == r.getCode()){
                if(r.getMessage().contains("已存在")){
                    hyMbDriver.setUploadStatus("1");
                    this.saveOrUpdateMbDriver(hyMbDriver);
                    return true;
                }
            }

            if (r.isSuccess() && 200 == r.getCode()) {
                hyMbDriver.setUploadStatus("1");
                this.saveOrUpdateMbDriver(hyMbDriver);
                return true;
            }else{
                hyMbDriver.setUploadStatus("2");
                hyMbDriver.setUploadFailureReason(r.getMessage());
                this.saveOrUpdateMbDriver(hyMbDriver);
                throw new JeecgBootException("该司机推送异常 " +r.getMessage());
            }
        }
    }

    public  MbDriverDTO driverTOMbDriverDTO(HyDriver driver) {
        if (driver == null) {
            return null;
        }
        MbDriverDTO mbDriverDTO = new MbDriverDTO();
        mbDriverDTO.setDriverName(driver.getDriverName());
        mbDriverDTO.setIdcard(driver.getIdcard());
        mbDriverDTO.setIdcardValidFrom(
                driver.getIdcardValidFrom().getTime()
        );
        mbDriverDTO.setIdcardExpiredDate(
                driver.getIdcardExpiredDate().getTime()
        );
        mbDriverDTO.setIdcardImg(
                maBangApiService.uploadFiles(  driver.getIdcardImg(), "司机")
        );
        mbDriverDTO.setIdcardImgBack(
                maBangApiService.uploadFiles(  driver.getIdcardImgBack(), "司机")
        );
        mbDriverDTO.setSex(driver.getSex());
        mbDriverDTO.setTelephone(driver.getTelephone());

        mbDriverDTO.setNation(driver.getNation());

        mbDriverDTO.setAddress(driver.getAddress());

        mbDriverDTO.setDrivingLicense(driver.getDrivingLicense());
        mbDriverDTO.setDriverImg(
                maBangApiService.uploadFiles( driver.getDriverImg() , "司机")
        );
        if(StringUtils.isNotBlank(driver.getDriverImg2() )){
            mbDriverDTO.setDriverImg2(
                    maBangApiService.uploadFiles( driver.getDriverImg2(), "司机")
            );
        }

        mbDriverDTO.setVehicleClass(driver.getVehicleClass());
        mbDriverDTO.setIssuingOrganizations(driver.getIssuingOrganizations());
        mbDriverDTO.setIssueStartDate(
                driver.getIssueStartDate().getTime()
        );
        mbDriverDTO.setValidPeriodFrom(
                driver.getValidPeriodFrom().getTime()
        );
        mbDriverDTO.setValidPeriodTo(
                driver.getValidPeriodTo().getTime()
        );
       // mbDriverDTO.setWeightType(driver.getWeightType());
        mbDriverDTO.setQualificationCertificate(driver.getQualificationCertificate());
        mbDriverDTO.setQualificationImg(
                maBangApiService.uploadFiles( driver.getQualificationImg(), "司机")
        );
        //mbDriverDTO.setQualificationExpiredDate(driver.getQualificationExpiredDate());
        mbDriverDTO.setIzAffiliation(driver.getIzAffiliation());
        mbDriverDTO.setAffiliationAgreement(
                driver.getAffiliationAgreement()
        );
        mbDriverDTO.setAreaCode(driver.getAreaCode());
        return mbDriverDTO;
    }



    @Override
    public boolean saveOrUpdateMbDriver(HyMbDriver hyMbDriver){
        try {
            hyMbDriver.setId(hyMbDriver.getDriverId());
            return this.saveOrUpdate(hyMbDriver);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }
}
